/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.trick.admin.controller;

import it.trick.admin.App;
import it.trick.admin.Constants;
import it.trick.admin.binding.AddChannelButtonBinding;
import it.trick.admin.binding.SelectChannelProgressBinding;
import it.trick.admin.model.ChannelModel;
import it.trick.admin.task.AddChannelTask;
import it.trick.admin.task.ChangeContentsIndexTask;
import it.trick.admin.task.DeleteChannelTask;
import it.trick.admin.task.LoadChannelListTask;
import it.trick.admin.task.UpdateChannelTask;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;

/**
 * FXML Controller class
 *
 * @author takahara
 */
public class SelectChannelController extends AnchorPane implements Initializable {

    private static SelectChannelController instance;

    @FXML
    private ComboBox siteComboBox;
    @FXML
    private ListView channelListView;
    @FXML
    private TextField channelKeyTextField;
    @FXML
    private TextField channelNameTextField;
    @FXML
    public HBox progressIndicatorHBox;
    @FXML
    public Button addButton;

    // -----------------------------タスク---------------------------------------
    public LoadChannelListTask loadChannelListTask = new LoadChannelListTask();
    public AddChannelTask addChannelTask = new AddChannelTask();
    public DeleteChannelTask deleteChannelTask = new DeleteChannelTask();
    public UpdateChannelTask updateChannelTask = new UpdateChannelTask();
    public ChangeContentsIndexTask changeIndexTask = new ChangeContentsIndexTask();

    /**
     * コンストラクタ
     */
    public SelectChannelController() {
        init();
    }

    /**
     * コントロールの初期化
     */
    private void init() {

        // ---------------------------------------------------
        // FXMLのロードと設定
        // ---------------------------------------------------
        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource("resource/fxml/select_channel_view.fxml"));
        fxmlLoader.setRoot(this);
        // 自分自身をコントロールとして設定
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }

        instance = this;
    }

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        progressIndicatorHBox.visibleProperty().bind(
                new SelectChannelProgressBinding(
                loadChannelListTask,
                addChannelTask,
                deleteChannelTask,
                updateChannelTask,
                changeIndexTask));

        // ---------------------------------------------------------------------
        // サービスの正常処理イベント
        // ---------------------------------------------------------------------
        loadChannelListTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
            @Override
            public void handle(WorkerStateEvent t) {

                // サービスの処理結果を受け取る
                List<ChannelModel> channelList = (List<ChannelModel>) t.getSource().getValue();
                channelListView.getItems().setAll(channelList);
            }
        });

        siteComboBox.setPromptText("Select Site");
        siteComboBox.getItems().setAll(Constants.SITES);
        siteComboBox.valueProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue ov, String t, String t1) {
                App.getInstance().setSite(t1);
                loadChannelListTask.restart();
            }
        });

        addButton.disableProperty().bind(new AddChannelButtonBinding(
                siteComboBox,
                channelKeyTextField,
                channelNameTextField));

    }

    @FXML
    public void addAction() {
        ChannelModel model = new ChannelModel();
        model.setKeyValueString(channelKeyTextField.getText());
        model.setName(channelNameTextField.getText());
        model.setIndex(channelListView.getItems().size() + 1);

        addChannelTask.setup(model);
        addChannelTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
            @Override
            public void handle(WorkerStateEvent t) {

                // サービスの処理結果を受け取る
                ChannelModel channelModel = (ChannelModel) t.getSource().getValue();

                // Tree Root Nodeを設定する
                channelListView.getItems().add(channelModel);

                channelKeyTextField.setText(null);
                channelNameTextField.setText(null);
            }
        });

        // ---------------------------------------------------------------------
        // サービスの例外イベント
        // ---------------------------------------------------------------------
        addChannelTask.setOnFailed(new EventHandler<WorkerStateEvent>() {
            @Override
            public void handle(WorkerStateEvent t) {
                Exception ex = (Exception) addChannelTask.getException();
                App.getInstance().showErrorPop(ex);
            }
        });

        addChannelTask.restart();
    }

    /**
     * Get App Instance
     *
     * @return
     */
    public static SelectChannelController getInstance() {
        return instance;
    }
}
